package cn.livingCloud.service.impl;

import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.dto.*;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.core.EmployeeWechat;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.repository.EmployeeWechatMapper;
import cn.livingCloud.repository.MemberMapper;
import cn.livingCloud.repository.UserAccountMapper;
import cn.livingCloud.service.EmployeeService;
import cn.livingCloud.service.impl.operation.EmployeeFormDtoUpdater;
import cn.livingCloud.service.impl.operation.EmployeeFromDtoLoader;
import cn.livingCloud.utils.SessionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 唐旭 on 2016/5/30.
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeWechatMapper wechatMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;


    @Override
    public Employee getEmployee(String employeeUuid) {
        return employeeMapper.selectByPrimaryKey(employeeUuid);
    }

    @Override
    public void loadEmployeeListDto(EmployeeListDto listDto) {
        String deptUuid = listDto.getDeptUuid();
        if (StringUtils.isEmpty(deptUuid)) {
            Employee employee = SessionUtils.getAttribute(Employee.class, SessionConstants.EMPLOYEE_KEY);
            if (employee != null) {
                listDto.setDeptUuid(employee.getDeptUuid());
            }
        }
        listDto.addAllList(employeeMapper.getList(listDto));
    }

    @Override
    public void loadEmployeeFromDto(EmployeeFormDto formDto) {
        EmployeeFromDtoLoader formDtoLoader = new EmployeeFromDtoLoader(formDto);
        formDtoLoader.loadFormDto();
    }

    @Override
    public void edit(EmployeeFormDto formDto) {
        EmployeeFormDtoUpdater updater = new EmployeeFormDtoUpdater(formDto);
        updater.update();
    }

    @Override
    public void delete(String uuid) {
        //禁用员工
        Employee employee = employeeMapper.selectByPrimaryKey(uuid);
        employee.setArchive(true);
        employeeMapper.updateByPrimaryKey(employee);
        //禁用员工微信号
        List<EmployeeWechat> byEmployeeUuid = wechatMapper.findByEmployeeUuid(uuid);
        for (EmployeeWechat employeeWechat : byEmployeeUuid) {
            employeeWechat.setArchive(true);
            wechatMapper.updateByPrimaryKey(employeeWechat);
        }
        //禁用员工账号
        List<UserAccount> list = userAccountMapper.findUserAccountByEmployeeUuid(uuid);
        for (UserAccount userAccount : list) {
            userAccount.setEnable(false);
            userAccountMapper.updateEnable(userAccount);
        }
    }

    @Override
    public EmployeeBindMemberDto loadBindMemberDto(String employeeUuid) {
        Employee employee = employeeMapper.selectByPrimaryKey(employeeUuid);
        return new EmployeeBindMemberDto(employee);
    }

    @Override
    public boolean checkMemberBound(String memberUuid) {
        List<EmployeeWechat> list = wechatMapper.findByMemberUuid(memberUuid);
        return list.size() > 0;
    }

    @Override
    public void bindMember(EmployeeBindMemberDto formDto) {
        EmployeeWechat wechat = new EmployeeWechat();
        wechat.setEmployeeUuid(formDto.getEmployeeUuid());
        wechat.setMemberUuid(formDto.getMemberUuid());
        wechatMapper.insert(wechat);
    }

    @Override
    public List<EmployeeJsonDto> findEmployees(String key) {
        if (StringUtils.isEmpty(key)) {
            return Collections.emptyList();
        }
        List<EmployeeJsonDto> result = new ArrayList<>();
        List<Employee> employees = employeeMapper.searchEmployee(key);
        for (Employee emp : employees) {
            result.add(new EmployeeJsonDto(emp));
        }
        return result;
    }

    @Override
    public Employee loadEmployeeByMember(String uuid) {
        return employeeMapper.findEmployeeByMember(uuid);
    }

    @Override
    public EmployeeRelieveMemberDto loadEmployeeRelieveMemberDto(String employeeUuid) {
        List<MemberDto> list = new ArrayList<>();
        List<EmployeeWechat> byEmployeeUuid = wechatMapper.findByEmployeeUuid(employeeUuid);
        for (EmployeeWechat employeeWechat : byEmployeeUuid) {
            Member memberByUuid = memberMapper.findMemberByUuid(employeeWechat.getMemberUuid());
            list.add(new MemberDto(memberByUuid));
        }
        return new EmployeeRelieveMemberDto(list);
    }

    @Override
    public JsonDto RelieveMember(EmployeeRelieveMemberDto formDto) {
        wechatMapper.deleteByMemberUuid(formDto.getMemberUuid());
        JsonDto jsonDto = new JsonDto();
        jsonDto.setState(true);
//        jsonDto.setMsg(msg);
        return jsonDto;
    }
}
